package com.zhangyuji.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.dto.DishDto;
import com.zhangyuji.entity.Dish;
import com.zhangyuji.entity.DishFlavor;
import com.zhangyuji.entity.SetmealDish;
import com.zhangyuji.service.DishFlavorService;
import com.zhangyuji.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R<IPage> pageDish(int page,int pageSize,String name){
        IPage<Dish> ipage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(ipage,queryWrapper);
        return R.success(ipage);
    }

    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable Long id){
        System.out.println("queryDishById"+id);
        /*Dish dish = dishService.getById(id);
        System.out.println(dish);*/
        DishDto dishDto = dishService.queryDishAndFlavor(id);
        if (dishDto != null){
            return R.success(dishDto);
        }
        return R.error("查询失败");
    }

    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto){
        System.out.println(dishDto);
        System.out.println(dishDto.getCategoryId());
        MyCurrentId.setCurrentId(dishDto.getCategoryId());
        boolean flag = dishService.saveDishAndFlavor(dishDto);
        return flag? R.success("添加成功") : R.error("添加失败");
    }

    @PostMapping("/status/{status}")
    public R<String> dishStatusByStatus(@PathVariable Integer status, Long[] ids, HttpServletRequest request){
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        List<Dish> dishList = new ArrayList<>();
        for (Long id : ids) {
            System.out.println(id);
            Dish dish = dishService.getById(id);
            dish.setStatus(status);
            System.out.println(dish.getStatus());
            dishList.add(dish);
        }
        for (Dish dish : dishList) {
            System.out.println(dish);
        }

        MyCurrentId.setCurrentId((Long) request.getSession().getAttribute("employee"));
        boolean flag = dishService.updateBatchById(dishList);
        return flag? R.success("修改成功") : R.error("修改失败");
    }

    @DeleteMapping
    public R<String> deleteDish(Long[] ids){
        System.out.println(ids);
        List<Long> idList = new ArrayList<>();
        for (Long id : ids) {
            idList.add(id);
        }
        boolean flag = dishService.removeByIds(idList);

        return flag? R.success("删除成功") : R.error("删除失败");
    }

    @PutMapping
    public R<String> editDish(@RequestBody DishDto dishDto){
        System.out.println(dishDto.getId());
        System.out.println(dishDto.getCategoryId());
        System.out.println(dishDto);
        MyCurrentId.setCurrentId(dishDto.getCategoryId());
        boolean flag = dishService.updateDishAndFlavor(dishDto);
        return flag? R.success("修改成功") : R.error("修改失败");
    }

    @GetMapping("/list")
    public R<List<DishDto>> queryDishList(Dish dish){
        log.info("categoryId:{}",dish.getCategoryId());
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId())
                    .eq(dish.getStatus() != null,Dish::getStatus,dish.getStatus());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<DishDto> dishDtoList = new ArrayList<>();

        List<Dish> dishList = dishService.list(queryWrapper);
        if (dishList.size() > 0){
            for (Dish d : dishList) {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(d,dishDto);
                //获取每一个dish的id
                Long dishId = d.getId();
                //根据dish_id查询口味
                LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorQueryWrapper.eq(DishFlavor::getDishId,dishId);
                List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorQueryWrapper);
                dishDto.setFlavors(dishFlavorList);
                dishDtoList.add(dishDto);

            }

        }

        System.out.println(dishDtoList);
        return R.success(dishDtoList);
    }
}

